
//dom对象
let subDom = document.querySelector('.sub');
let textInp = document.querySelector('input[type="text"]');
let optDom = document.querySelector('select');
let textArea = document.querySelector('textarea');
let userInfo = getCookie('userInfo') ? JSON.parse(decodeURIComponent(getCookie('userInfo'))) : null;
// console.log(userInfo);
//保存定时器标识
let timer;
// 倒计时初始值
let time = 3;

// clearInterval(timer);


// 当显示这个页面时如果localstorage中存在数据 就将数据显示在textarea中
document.querySelector('textarea').value = localStorage.getItem('content_');

//上传图片
layui.use(['upload', 'element', 'layer'], function () {
    var $ = layui.jquery
        , upload = layui.upload
        , element = layui.element
        , layer = layui.layer;
    //常规使用 - 普通图片上传
    var uploadInst = upload.render({
        elem: '#test1'
        , url: 'http://39.98.112.34:8000/uploads'
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#demo1').attr('src', result); //图片链接（base64）
            });

            element.progress('demo', '0%'); //进度条复位
            layer.msg('上传中', { icon: 16, time: 0 });
        }
        //进度条
        , progress: function (n, elem, e) {
            element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
            if (n == 100) {
                layer.msg('上传完毕', { icon: 1 });
            }
        }
    });
})

//是否登录
if (userInfo) {     // 登录状态
    //登录按钮状态
    document.querySelector('.resgist').innerHTML = `${userInfo.nickname} 欢迎`;
    //退出按钮
    document.querySelector('.out').addEventListener('click', () => {
        //删除cookie
        removeCookie('userInfo');
        //重新加载
        location.reload();
    })

    //发布按钮
    subDom.addEventListener('click', async () => {
        //本地图片地址
        let imgUrl = document.querySelector('#demo1').src
        let res =await axios({
            method: 'post',
            url: 'http://39.98.112.34:8000/introduction/create',
            data: {
                scenicId: optDom.value,
                title: textInp.value,
                coverImg: imgUrl,
                content: textArea.value
            },
            headers: { 'Authorization': 'bearer ' + userInfo.sessionKey }
        })
        console.log(res)

        // 发布成功清除localStorage中的数据
        localStorage.removeItem('content_');
        layer.msg('发布成功，3s后返回查看详情', { icon: 6 });
        timer = setInterval(() => {
            time--;
            if (time < 0) {
                location.href = 'raiders.html'
            }
        }, 1000)
    })

} else {
    //登录按钮
    document.querySelector('.resgist').innerHTML = '登录';
    document.querySelector('.resgist').onclick = () => {
        location.href = 'login.html?callback=' + encodeURIComponent(location.href);
    }
    //发布按钮
    document.querySelector('.sub').innerHTML = `登录后发布`;
    document.querySelector('.sub').onclick = () => {
        //没有登录，需要将用户输入的内容保存起来

        localStorage.setItem('content_', textArea.value);
        // 跳转到登录页
        location.href = 'login.html?callback=' + encodeURIComponent(location.href);
    }
}

//取消按钮
document.querySelector('.lifted').onclick = () => {
    location.href = 'index.html'
}
